﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraTreeList.Columns;
using Inaction.UI.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Inaction.UI.Extensions.ControlExtensions
{
    public static class LookUpEditBaseExtender
    {
        public static void Extend(Control c, Form form)
        {
            if (!(c is LookUpEditBase)) return;
            var lookUp = c as LookUpEditBase;
            lookUp.Properties.ImmediatePopup = true;
            lookUp.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            lookUp.Properties.NullText = string.Empty;
            lookUp.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
            lookUp.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Space); 

            if(lookUp.Properties.DataSource!=null)return;

            #region 下拉列表设置默认数据源
            var binding = c.DataBindings.Cast<Binding>().FirstOrDefault(p => p.PropertyName == "EditValue");
            if (binding != null && binding.DataSource is BindingSource)
            {
                var bs = binding.DataSource as BindingSource;
                var type = BindingHelper.GetListItemType(bs);
                if (type.GetInterface(typeof(Inaction.Core.ISavable).FullName) == null
                    && !type.IsSubclassOf(typeof(Criteria.CriteriaBase))) return;
                var property = type.GetProperty(binding.BindingMemberInfo.BindingMember);
                if (property != null)
                {
                    var pType = property.PropertyType;
                    if (pType.IsValueType)
                    {
                        if (pType.IsGenericType)
                        {
                            pType = System.Nullable.GetUnderlyingType(pType);
                        }
                    }

                    #region 枚举/布尔
                    if (c is LookUpEdit && (pType.IsEnum || pType == typeof(Boolean)))
                    {
                        var lue = c as LookUpEdit;
                        if (pType == typeof(Boolean))
                        {
                            pType = typeof(Rule.BooleanEnum);
                        }
                        var source = Rule.EnumDescriptionList.Query(pType);
                        if (source != null)
                        {
                            lue.Properties.DataSource = source;
                            lue.Properties.DisplayMember = "Description";
                            lue.Properties.ValueMember = "Value";
                            lue.Properties.Columns.Clear();
                            lue.Properties.Columns.Add(new LookUpColumnInfo("Key", "键"));
                            lue.Properties.Columns.Add(new LookUpColumnInfo("Description", "说明"));
                        }
                    }
                    #endregion

                    #region 外键

                    #endregion
                }
            }
            #endregion
        }
    }
}
